package tankwar4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @Auther: Tx
 * @Date: 2022/7/14 11:26
 */
//为了监听键盘事件，实现keyListener
class MyPanel extends JPanel implements KeyListener, Runnable {
    myTank myTank = null;//初始化自己的坦克

    Vector<EnemyTank> enemyTanks = new Vector<>();
    //存放爆炸效果
    Vector<Boom> booms = new Vector<>();

    //    定义三张图片，用于显示爆炸效果
    Image image1 = null;
    Image image2 = null;
    Image image3 = null;
    //敌人坦克
    int enemyTankNum = 3;

    //初始化敌方坦克数量
    public MyPanel() {
        myTank = new myTank(500, 500);
        for (int i = 0; i < enemyTankNum; i++) {
//            创建一个敌人坦克
            EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 100);
//          设置方向
            enemyTank.setDirection(2);
//            给敌人坦克加入一颗子弹
            Bullet bullet = new Bullet(enemyTank.getX(), enemyTank.getY(), enemyTank.getDirection());
//            使敌人坦克动起来
            new Thread(enemyTank).start();
            enemyTank.bullets.add(bullet);
            new Thread(bullet).start();
            //将敌人坦克加入
            enemyTanks.add(enemyTank);
        }
//        初始化图片对象
        image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_1.gif"));
        image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_2.gif"));
        image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bomb_3.gif"));
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 1000, 750);
        drawTank(myTank.getX(), myTank.getY(), g, myTank.getDirection(), 0);

////        画出坦克的子弹
//        if (myTank.bullet != null && myTank.bullet.isLive) {
//            g.draw3DRect(myTank.bullet.x, myTank.bullet.y, 5, 5, false);
//        }
        for (int i = 0; i < myTank.bullets.size(); i++) {
            if (myTank.bullets != null && myTank.bullets.get(i).isLive) {
                g.draw3DRect(myTank.bullets.get(i).x, myTank.bullets.get(i).y, 5, 5, false);
            } else {
                myTank.bullets.remove(myTank.bullets.get(i));
            }
        }


//绘制敌方坦克的同时绘制子弹
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            if (enemyTank.isLive) {
                //当坦克存活时才绘制
                drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
                //画出所有子弹
                for (int j = 0; j < enemyTank.bullets.size(); j++) {
                    if (enemyTank.bullets.get(j) != null && enemyTank.bullets.get(j).isLive) {
                        g.draw3DRect(enemyTank.bullets.get(j).x + 20, enemyTank.bullets.get(j).y, 5, 5, false);
                    }
//               当子弹已经超出范围时，从集合中移除
                    else {
                        enemyTank.bullets.remove(enemyTank.bullets.get(j));
                    }
                }


            }

        }

        //                绘制爆炸效果
        for (int i = 0; i < booms.size(); i++) {
            Boom boom = booms.get(i);
//            根据life值去画出对应的图像
            if (boom.life > 6) {
                g.drawImage(image1, boom.x, boom.y, 60, 60, this);
            } else if (boom.life > 3) {
                g.drawImage(image2, boom.x, boom.y, 60, 60, this);

            } else {
                g.drawImage(image3, boom.x, boom.y, 60, 60, this);
            }
            boom.lifeDown();
            if (boom.life == 0) {
                booms.remove(boom);
            }
        }
    }


    public void drawTank(int x, int y, Graphics g, int direct, int type) {
        switch (type) {
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        //根据坦克方向，来绘制坦克
        switch (direct) {
            case 0://绘制向上的坦克
                g.fill3DRect(x, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 10, y + 15, 20, 30, false);
                g.fillOval(x + 10, y + 20, 20, 20);//坦克的盖子
                g.drawLine(x + 20, y + 30, x + 20, y);//坦克的炮筒
                repaint();
                break;
            case 1://绘制向右的坦克
                g.fill3DRect(x, y, 60, 10, false);//画上边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画下边的轮子
                g.fill3DRect(x + 15, y + 10, 30, 20, false);//坦克的中间部分
                g.fillOval(x + 20, y + 10, 20, 20);//坦克的盖子
                g.drawLine(x + 30, y + 20, x + 60, y + 20);//坦克的炮筒

                break;
            case 2://绘制向下的坦克
                g.fill3DRect(x, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 30, y, 10, 60, false);//画左边的轮子
                g.fill3DRect(x + 10, y + 15, 20, 30, false);
                g.fillOval(x + 10, y + 20, 20, 20);//坦克的盖子
                g.drawLine(x + 20, y + 30, x + 20, y + 60);//坦克的炮筒
                break;
            case 3:
                g.fill3DRect(x, y, 60, 10, false);//画上边的轮子
                g.fill3DRect(x, y + 30, 60, 10, false);//画下边的轮子
                g.fill3DRect(x + 15, y + 10, 30, 20, false);//坦克的中间部分
                g.fillOval(x + 20, y + 10, 20, 20);//坦克的盖子
                g.drawLine(x + 30, y + 20, x, y + 20);//坦克的炮筒
                break;
        }


    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            myTank.setDirection(0);
            //修改坦克的坐标
            if (myTank.getY() > 0)
                myTank.moveUp();

        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            myTank.setDirection(1);
            if (myTank.getX() + 60 < 1000)
                myTank.moveRight();

        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            myTank.setDirection(2);
            if (myTank.getY() + 60 < 750)
                myTank.moveDown();

        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            myTank.setDirection(3);
            if (myTank.getX() > 0)
                myTank.moveLeft();

        }

        if (e.getKeyCode() == KeyEvent.VK_J) {
//            if (myTank.bullet==null||!myTank.bullet.isLive)
//            myTank.shot();
            myTank.shot();
        }
//        面板重绘
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            hitEnemyTank();
            hitMytank();
//            判断敌人子弹是否
            this.repaint();
        }
    }

    public void hitMytank() {
        for (int i = 0; i < enemyTanks.size(); i++) {
//        取出敌人坦克
            EnemyTank enemyTank = enemyTanks.get(i);
//遍历敌人坦克的所有子弹
            for (int j = 0; j < enemyTank.bullets.size(); j++) {
                Bullet bullet = enemyTank.bullets.get(j);
//            判断是否击中我方坦克，根据不同方向判断
                if (myTank.isLive() && bullet.isLive) {
                    hitTank(bullet, myTank);
                }

            }
        }


    }
//击中敌人坦克

    public void hitEnemyTank() {

        for (int j = 0; j < myTank.bullets.size(); j++) {
//            判断是否击中了敌人坦克
            if (myTank.bullet != null && myTank.bullet.isLive) {
//                遍历所有的敌人坦克
                for (int i = 0; i < enemyTanks.size(); i++) {
                    EnemyTank enemyTank = enemyTanks.get(i);
                    hitTank(myTank.bullets.get(j), enemyTank);
                }

            }
        }
    }

    public void hitTank(Bullet bullet, Tank tank) {
        //判断子弹击中坦克
//        根据地方坦克的方向不同判断击中的范围
        switch (tank.getDirection()) {
            case 0:
            case 2:
                if (bullet.x >= tank.getX() && bullet.x <= tank.getX() + 40 &&
                        bullet.y >= tank.getY() && bullet.y <= tank.getY() + 60) {
                    bullet.isLive = false;
                    tank.setLive(false);
                    myTank.bullet.isLive = false;
                    enemyTanks.remove(tank);
                    booms.add(new Boom(tank.getX(), tank.getY()));
                    break;
                }

            case 1:
            case 3:
                if (bullet.x <= tank.getX() && bullet.x >= tank.getX() + 60 ||
                        bullet.y <= tank.getY() && bullet.y >= tank.getY() + 40) {
                    bullet.isLive = false;
                    tank.setLive(false);
                    myTank.bullet.isLive = false;
                    enemyTanks.remove(tank);
                    booms.add(new Boom(tank.getX(), tank.getY()));
                    break;
                }


        }
    }
}
